﻿using Common;
using GlobalVariableModuleCs;
using System;
using VM.Core;
using VM.PlatformSDKCS;
using VMControls.WPF.Release;

namespace CarPCBATest.Procedure
{
    internal class VisionTool
    {
        public static string GetGlobalVar(string name)
        {
            try
            {
                // 4.5 修改全局变量
                GlobalVariableModuleTool global = VmSolution.Instance["全局变量1"] as GlobalVariableModuleTool;
                if (global != null)
                {
                    var tmp = global.GetGlobalVar(name);
                    Mylog.Debug($"vmGet全局变量 [{name}]=[{tmp}]");
                    return tmp;
                }
                return null;
            }
            catch (VmException vex)
            {
                Mylog.Error(vex, vex.Message);
                throw new Exception(vex.Message);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                throw ex;
            }
        }

        public static void SetGlobalVar(string name, string value)
        {
            try
            {            // 4.5 修改全局变量
                GlobalVariableModuleTool global = VmSolution.Instance["全局变量1"] as GlobalVariableModuleTool;
                if (global != null)
                {
                    global.SetGlobalVar(name, value);
                    Mylog.Info($"vmSet全局变量 [{name}]=[{value}]");
                }
            }
            catch (VmException vex)
            {
                Mylog.Error(vex, vex.Message);
                throw new Exception(vex.Message);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                throw ex;
            }
        }

        public static void LoadVMSolution(string path)
        {
            try
            {
                VmSolution.Load(path);
            }
            catch (VmException vex)
            {
                Mylog.Error(vex, vex.Message);
                throw new Exception(vex.Message);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 运行流程
        /// </summary>
        /// <param name="procName"> </param>
        public static void RunProc(string procName)
        {
            try
            {
                VmProcedure vmProcedure = VmSolution.Instance[procName] as VmProcedure;
                if (null == vmProcedure) throw new Exception($"未找到流程[{procName}]");
                vmProcedure.Run();
            }
            catch (VmException vex)
            {
                Mylog.Error(vex, vex.Message);
                throw new Exception(vex.Message);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                throw ex;
            }
        }

        public static void SetProcVisionRet(string procName, VmRenderControl control)
        {
            try
            {
                //Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                //{
                //}));
                VmProcedure vmProcedure = VmSolution.Instance[procName] as VmProcedure;
                if (null == vmProcedure) throw new Exception($"未找到流程[{procName}]");

                control.ModuleSource = vmProcedure;
            }
            catch (VmException vex)
            {
                Mylog.Error(vex, vex.Message);
                throw new Exception(vex.Message);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                throw ex;
            }
        }
    }
}